package com.itheima.注解.注解案例;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Demo {
    @Autowired("ArrayList")
    private static List list;

    static {
        try {
            //拿到当前类的字节码对象
            Class<Demo> demoClass = Demo.class;
            //拿到list对应的field对象
            Field listField = demoClass.getDeclaredField("list");
            //判断field上是否有注解
            if (listField.isAnnotationPresent(Autowired.class)) {
                //取出注解中的值
                Autowired autowired = listField.getAnnotation(Autowired.class);
                String value = autowired.value();
                //判断该属性值
                if ("LinkedList".equals(value)) {
                    list = new LinkedList();
                }else if ("ArrayList".equals(value)){
                    list = new ArrayList();
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        System.out.println(list.getClass());
    }
}
